package com.fos.BluetoothStory.util;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Media;

import com.fos.BluetoothStory.model.MusicBean;

/**
 * 获取本地音乐数据
 * @author fanxing 创建于 Oct 22, 2015
 */
public class LocalSongsUtil {
	private String[] projection = {  
			Media._ID,  
			Media.DISPLAY_NAME,  
			Media.DATA,  
			Media.ALBUM,  
			Media.ARTIST,  
			Media.DURATION,           
			Media.SIZE  
	};  
	private String where =  "mime_type in ('audio/mpeg','audio/x-ms-wma') and bucket_display_name <> 'audio' and is_music > 0 " ;

	public static ArrayList<MusicBean> getLocalSongsInfos(ContentResolver contentResolver) {
		ArrayList<MusicBean> infos = new ArrayList<MusicBean>();
		
		Cursor cursor = null;
//		String sdstate = Environment.getExternalStorageState();
//		if(Environment.MEDIA_MOUNTED.equals(sdstate)){
			//sdcard
			cursor = contentResolver.query(
					Media.EXTERNAL_CONTENT_URI, null, null, null,
					Media.DEFAULT_SORT_ORDER);
			
			infos.addAll(extraCursor(cursor));
//		}
        
        //内部卡,与上表数据重复
//        cursor = contentResolver.query(
//                MediaStore.Audio.Media.INTERNAL_CONTENT_URI, null, null, null,
//                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
//        
//        infos.addAll(extraCursor(cursor));
        
        return infos;
    }

	private static ArrayList<MusicBean> extraCursor(Cursor cursor) {
		ArrayList<MusicBean> infos = new ArrayList<MusicBean>();
        
        if(cursor==null || !cursor.moveToFirst())
        	return infos;
        
        for (int i = 0; i < cursor.getCount(); i++) {
        	MusicBean songInfo = new MusicBean();                               //新建一个歌曲对象,将从cursor里读出的信息存放进去,直到取完cursor里面的内容为止.

            int id = cursor.getInt(cursor
                    .getColumnIndex(Media._ID));	//音乐id

            String title = cursor.getString((cursor
                    .getColumnIndex(Media.TITLE)));//音乐标题

            String artist = cursor.getString(cursor
                    .getColumnIndex(Media.ARTIST));//艺术家

            long duration = cursor.getLong(cursor
                    .getColumnIndex(Media.DURATION));//时长

            long size = cursor.getLong(cursor
                    .getColumnIndex(Media.SIZE));	//文件大小

            String url = cursor.getString(cursor
                    .getColumnIndex(Media.DATA));	//文件路径

            String album = cursor.getString(cursor
                    .getColumnIndex(Media.ALBUM)); //唱片图片

            long album_id = cursor.getLong(cursor
                    .getColumnIndex(Media.ALBUM_ID)); //唱片图片ID

            int isMusic = cursor.getInt(cursor
                    .getColumnIndex(Media.IS_MUSIC));//是否为音乐

            if (isMusic != 0 && duration/(1000 * 60) >= 1) {		//只把1分钟以上的音乐添加到集合当中
            	songInfo.pid = MusicBean.LPID;
                songInfo.id = id;
                songInfo.title = title;
                songInfo.artist = artist;
                songInfo.duration = duration;
                songInfo.size = size;
                songInfo.url = url;
                songInfo.album = album;
                infos.add(songInfo);
            }
            
            cursor.moveToNext();
        }
        return infos;
	}
	
}
